<?php # Фильтр
class store_product_filter extends Load_use
{
	function getBlock()
	{
		$characts_html = $brand_html = $brand_list = '';
		$characts = array();
		$parents = $this->view->parents.'#'.$this->view->id.'#';


		$this->conn->query('SELECT * FROM `pages` WHERE `templ` = "sub_category" AND `parents` = "'.$parents.'"');
		if( $this->conn->rowCount() )
			return false;

		$brand_ids = isset( $_GET['br'] ) ? $_GET['br'] : array();
		if( $this->view->brand_id )
			$brand_ids[] = $this->view->brand_id;

			// Бренды
		$this->conn->query('SELECT `p`.`brand_id`, `b`.`name`, COUNT( `p`.`brand_id` ) AS `count_brand`
								FROM `store_products_cache` AS `p`
									JOIN `store_product_brand` AS `b` ON `b`.`id` = `p`.`brand_id`
										WHERE `p`.`parents` = "'.$parents.'" AND `p`.`price_retail` > 0 AND `p`.`pp` IS NOT NULL
											GROUP BY `p`.`brand_id`
												ORDER BY `b`.`name`');
		$i = 0;
		while( $row = $this->conn->fetch() )
		{
			$checked = in_array( $row->brand_id, $brand_ids ) ? 'checked="checked"' : '';
			$brand_list .= '<li'.( ( $i++ == 0 ) ? ' class="first-child"' : '' ).'>
								<input id="'.$row->brand_id.'" name="br['.$row->name.']" value="'.$row->brand_id.'" '.$checked.' type="checkbox" />
								<label for="'.$row->brand_id.'">'.$row->name.'</label>
								<span class="count_values">('.$row->count_brand.')<span>
							</li>';
		}
		$brand_html .= 	'<div class="filter-list__inner">
							<h3>Производитель</h3>
							<ul class="filter-list__inner__brands">
								'.$brand_list.'
							</ul>
						 </div>';


			// Цены
		$this->conn->query('SELECT MIN( `price_retail` ) AS `min_price`, MAX( `price_retail` ) AS `max_price` FROM `store_products_cache`
								WHERE `parents` = "'.$parents.'" AND `price_retail` > 0 AND `pp` IS NOT NULL' );
							
		$min_price = $max_price = 0;
		if( $row = $this->conn->fetch() ) {
			if( $row->min_price == $row->max_price )
				return false;
			$min_price = $row->min_price;
			$max_price = $row->max_price;
		}
		
		$min_cost = isset( $_GET['mi'] ) ? $_GET['mi'] : $min_price;
		$max_cost = isset( $_GET['ma'] ) ? $_GET['ma'] : $max_price;


		/*$sql_get_filters = '
			SELECT `sf`.`name`, `v`.`value`, `d`.`field_id`, `v`.`id` AS value_id, COUNT( `v`.`id` ) AS count_values  FROM `store_p_charact_datas` AS `d`
			JOIN `store_products_cache` AS `p` ON `d`.`product_code` = `p`.`product_code` AND `p`.`parents` = "'.$parents.'" '.( count( $brand_ids ) ? 'AND `p`.`brand_id` IN( '.implode( ', ', $brand_ids ).' )' : '' ).'
			JOIN `store_product_filter` AS `f` ON `d`.`field_id` = `f`.`field_id` AND `f`.page_id = '.$this->view->id.'
			JOIN `store_p_charact_fields` AS `sf` ON `d`.`field_id` = `sf`.`id`
			JOIN `store_p_charact_values` AS `v` ON `d`.`value_id` = `v`.`id`
			WHERE `p`.`price_retail` > 0 AND `p`.`pp` IS NOT NULL
			GROUP BY `sf`.`id`, `v`.`id`
			ORDER BY `sf`.`name`, length(`v`.`value`), `v`.`value`
		';*/

			// Характеристики
		/*$path = __DIR__ .'/cache/filter/'. md5(strtok($_SERVER['REQUEST_URI'], '?')).'.txt';
		echo md5($_SERVER['REQUEST_URI']);


		if(file_exists($path) && (time() - filemtime($path)) <= 60*60*24) {
			$characts_html = file_get_contents($path);

		} else {
			$this->conn->query($sql_get_filters);

			while( $row = $this->conn->fetch() )
				$characts[$row->field_id][$row->value_id] = array( 'name' => $row->name, 'value' => $row->value, 'count_values' => $row->count_values, );

			foreach( $characts as $field_id => $charact )
			{
				$i = 0;
				$charact_list = $checked = '';
				if( count( $charact ) > 1 )
				{
					foreach( $charact as $value_id => $charac )
					{
						$checked = ( isset( $_GET['ch'][$field_id] ) && in_array( $value_id, $_GET['ch'][$field_id] ) ) ? 'checked="checked"' : '';
						$charact_list .= '
						<li'.( ( $i++ == 0 ) ? ' class="first-child"' : '' ).'>
							<input name="ch['.$field_id.'][]" value ="'.$value_id.'" type="checkbox" id="'.$field_id.'_'.$value_id.'" '.$checked.' />
							<label for="'.$field_id.'_'.$value_id.'">'.$charac['value'].'</label>
							<span class="count_values">('.$charac['count_values'].')<span>
						</li>';

					}
					$characts_html .= ' <div class="filter-list__inner">
										<h3>'.$charac['name'].'</h3>
										<ul class="filter-list__inner__brands">'.$charact_list.'</ul>
									</div>';
				}
			}

			try {
				unlink($path);
			} catch (mysqli_sql_exception $e) {

				$error = $e;

			}

			file_put_contents($path, $characts_html);
		}*/


	
	
		
	
			// Статус
		/*$status = array(
			0 => 'Нет в наличии',
			1 => 'В наличии',
			3 => 'Под заказ',
		);
			
		$c = 0;
		$statuses = array();
		$this->conn->query('SELECT pp, COUNT(pp) AS count_pp FROM store_products_cache WHERE parents = "'.$parents.'" GROUP BY pp ORDER BY find_in_set(`pp`, "1,3,4,5,0")');
		while( $row = $this->conn->fetch() ) {
			if( in_array( $row->pp , array( 0, 1 ) ) )
				$statuses[$row->pp] = $row->count_pp;
			elseif( in_array( $row->pp, array( 3, 4, 5 ) ) ) {
				$c += $row->count_pp;
				$statuses[3] = $c;
			}
		}
		
		$i = 0;
		$status_html = $status_list = '';
		$pps = isset( $_GET['pp'] ) ? $_GET['pp'] : array();
		
		foreach( $statuses as $pp => $count_pp )
		{
			$checked = in_array( $pp, $pps ) ? 'checked="checked"' : '';
			$status_list .= '<li'.( ( $i++ == 0 ) ? ' class="first-child"' : '' ).'>
								<input id="pp_'.$pp.'" name="pp[]" value="'.$pp.'" '.$checked.' type="checkbox" />
								<label for="pp_'.$pp.'">'.$status[$pp].'</label>
								<span class="count_values">('.$count_pp.')<span>
							</li>';
		}
		if( $status_list )
			$status_html = 	'<div class="filter-list__inner">
								<h3>Статус</h3>
								<ul class="filter-list__inner__brands">
									'.$status_list.'
								</ul>
							 </div>';*/
						 
						 
						 
		
		$this->view->js_on_load[] = '
			/*- Скрытие/раскрытие фильтра -*/
			$(".filter-list__title").click(function(e)
			{
				if($(this).parent().hasClass("selected"))
				{
					$(this).parent().removeClass("selected");
				}
				else
				{
					//$(".filter-list__item").removeClass("selected");
					$(this).parent().addClass("selected");
				}
			});';
		
	
		

		return '<form class="filter-list" action="">
					<fieldset class="filter-list__item selected">
						<span class="filter-list__title">Фильтр товаров</span>
						<div class="filter-list__inner">
							<h3>Цена, руб</h3>
							<div class="horizontal-slider">
								<div class="formCost">
									<div class="formCost__left">
										<label for="price__min-cost">от</label>
										<input type="text" name="mi" id="price__min-cost" class="min-cost" value="'.$min_cost.'"/>
										<input type="hidden" name="min_price" id="min_price" value="'.$min_price.'"/>
									</div>
									<div class="formCost__right">
										<label for="price__max-cost">до</label>
										<input type="text" name="ma" id="price__max-cost" class="max-cost" value="'.$max_cost.'"/>
										<input type="hidden" name="max_price" id="max_price" value="'.$max_price.'"/>
									</div>
								</div>
								<div class="sliderCont">
									<div id="price" class="slider">&nbsp;</div>
								</div>
							</div>
						</div>
						'.$brand_html/*.$characts_html.$status_html*/.'
						<div class="filter-list__alert">
							<div class="filter-list__alert__inner">
								<ul>
									<li>Найдено <span class="filter_count_pr">0</span> товаров</li>
									<li><a href="#" class="filter_count_link">Показать</a></li>
								</ul>
								<span class="filter-list__alert__close">x</span>
							</div>
						</div>
						<input type="hidden" name="parents" value="'.$parents.'" />
						<input type="hidden" name="url" value="'.$this->view->url_before.'/'.$this->view->url.'" />
					</fieldset>
				</form>';
	}
}